home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 4111 / 4111.xpi / content / aardvarkCommands.js < prev    next >
Text File  |  2010-02-05  |  28KB  |  885 lines

  1. aardvark.loadObject ({
  2.  
  3. keyCommands : [],
  4.  
  5.  
  6. //------------------------------------------------------------
  7. loadCommands : function () {
  8. if (this.keyCommands.length > 0)
  9.   return;
  10. // 0: name (member of this.strings, or literal string)
  11. // 1: function
  12. // 2: no element needed (null for element commands)
  13. // 3: "extension" of ext only, "bookmarklet" for bm only, null for both
  14. var keyCommands = [
  15. ["wider", this.wider],
  16. ["narrower", this.narrower],
  17. ["undo", this.undo, true],
  18. ["quit", this.quit, true],
  19. ["remove", this.removeElement],
  20. ["kill", this.rip, null, "extension"],
  21. ["isolate", this.isolateElement],
  22. ["black on white", this.blackOnWhite],
  23. ["deWidthify", this.deWidthify],
  24. ["colorize", this.colorize],
  25. ["view source", this.viewSource],
  26. ["javascript", this.makeJavascript],
  27. ["paste", this.paste],
  28. ["help", this.showMenu, true],
  29. ["xpath", this.getElementXPath],
  30. ["global", this.makeGlobalFromElement]
  31. ];
  32.  
  33. for (var i=0; i<keyCommands.length; i++)
  34.   this.addCommand.apply (this, keyCommands[i]);
  35. },
  36.  
  37.  
  38. //-----------------------------------------------------
  39. addCommand : function (name, func,
  40.     noElementNeeded, mode, keystroke) {
  41. if (this.isBookmarklet) {
  42.   if (mode == "extension")
  43.     return;
  44.   }
  45. else {
  46.   if (mode == "bookmarklet")
  47.     return;
  48.   }
  49.     
  50. if (this.strings[name] && this.strings[name] != "")
  51.   name = this.strings[name];
  52.  
  53. if (keystroke) {
  54.   keyOffset = -1;
  55.   }
  56. else {
  57.   var keyOffset = name.indexOf('&');
  58.   if (keyOffset != -1) {
  59.     keystroke = name.charAt(keyOffset+1);
  60.     name = name.substring (0, keyOffset) + name.substring (keyOffset+1);
  61.     }
  62.   else {
  63.     keystroke = name.charAt(0);
  64.     keyOffset = 0;
  65.     }
  66.   }
  67. var command = {
  68.     name: name,
  69.     keystroke: keystroke,
  70.     keyOffset: keyOffset,
  71.     func: func
  72.     } 
  73. if (noElementNeeded)
  74.   command.noElementNeeded = true; 
  75. this.keyCommands.push (command);
  76. },
  77.  
  78.  
  79. //------------------------------------------------------------
  80. rip : function (elem) {
  81. if (window.RemoveItPermanently)
  82.   RemoveItPermanently.doRipNode(elem);
  83. else {
  84.   var dbox = new AardvarkDBox ("#fff", true);
  85.   dbox.innerContainer.innerHTML = this.strings.ripHelp;  
  86.   dbox.show ();
  87.   }
  88. return true;
  89. },
  90.  
  91. //------------------------------------------------------------
  92. wider : function (elem) {
  93. if (elem && elem.parentNode) {
  94.   var newElem = this.findValidElement (elem.parentNode);
  95.   if (!newElem)
  96.     return false;
  97.   
  98.   if (this.widerStack && this.widerStack.length>0 && 
  99.     this.widerStack[this.widerStack.length-1] == elem) {
  100.     this.widerStack.push (newElem);
  101.     }
  102.   else {
  103.     this.widerStack = [elem, newElem];
  104.     }
  105.   this.selectedElem = newElem;
  106.   this.showBoxAndLabel (newElem, 
  107.       this.makeElementLabelString (newElem));
  108.   this.didWider = true;
  109.   return true;
  110.   }
  111. return false;
  112. },
  113.  
  114. //------------------------------------------------------------
  115. narrower : function (elem) {
  116. if (elem) {
  117.   if (this.widerStack && this.widerStack.length>1 && 
  118.     this.widerStack[this.widerStack.length-1] == elem) {
  119.     this.widerStack.pop();
  120.     newElem = this.widerStack[this.widerStack.length-1];
  121.     this.selectedElem = newElem;
  122.     this.showBoxAndLabel (newElem, 
  123.         this.makeElementLabelString (newElem));
  124.     this.didWider = true;
  125.     return true;
  126.     }
  127.   }
  128. return false;
  129. },
  130.   
  131. //------------------------------------------------------------
  132. quit : function () {
  133. this.doc.aardvarkRunning = false;
  134.  
  135. if (this.doc.all) {
  136.   this.doc.detachEvent ("onmouseover", this.mouseOver);
  137.   this.doc.detachEvent ("onmousemove", this.mouseMove);
  138.   this.doc.detachEvent ("onkeypress", this.keyDown);
  139.   this.doc.detachEvent ("onmouseup", this.mouseUp, false);
  140.   }
  141. else {
  142.   this.doc.removeEventListener("mouseover", this.mouseOver, false);
  143.   this.doc.removeEventListener("mousemove", this.mouseMove, false);
  144.   this.doc.removeEventListener("mouseup", this.mouseUp, false);
  145.   this.doc.removeEventListener("keypress", this.keyDown, false);
  146.   }
  147.  
  148. this.removeBoxFromBody ();
  149.  
  150. delete (this.selectedElem);
  151. if (this.widerStack)
  152.   delete (this.widerStack);
  153. return true;
  154. },
  155.  
  156. //------------------------------------------------------------
  157. suspend : function () {
  158. if (this.doc.all) {
  159.   this.doc.detachEvent ("onmouseover", this.mouseOver);
  160.   this.doc.detachEvent ("onkeypress", this.keyDown);
  161.   }
  162. else {
  163.   this.doc.removeEventListener("mouseover", this.mouseOver, false);
  164.   this.doc.removeEventListener("keypress", this.keyDown, false);
  165.   }
  166. return true;
  167. },
  168.  
  169. //------------------------------------------------------------
  170.  
  171. resume : function () {
  172. if (this.doc.all) {
  173.   this.doc.attachEvent ("onmouseover", this.mouseOver);
  174.   this.doc.attachEvent ("onkeypress", this.keyDown);
  175.   }
  176. else {
  177.   this.doc.addEventListener ("mouseover", this.mouseOver, false);
  178.   this.doc.addEventListener ("keypress", this.keyDown, false);
  179.   }
  180. return true;
  181. },
  182.  
  183. //------------------------------------------------------------
  184.  
  185. viewSource : function (elem) {
  186. var dbox = new AardvarkDBox ("#fff", true, false, false, this.strings.viewHtmlSource, true);
  187. var v = this.getOuterHtmlFormatted(elem, 0);
  188. dbox.innerContainer.innerHTML = v;
  189.  
  190. if (!this.doc.didViewSourceDboxCss) {
  191.   this.createCSSRule ("div.aardvarkdbox div", "font-size: 13px; margin: 0; padding: 0;");
  192.   this.createCSSRule ("div.aardvarkdbox div.vsblock", "font-size: 13px; border: 1px solid #ccc; border-right: 0;margin: -1px 0 -1px 1em; padding: 0;");
  193.   this.createCSSRule ("div.aardvarkdbox div.vsline", "font-size: 13px; border-right: 0;margin: 0 0 0 .6em;text-indent: -.6em; padding: 0;");
  194.   this.createCSSRule ("div.aardvarkdbox div.vsindent", "font-size: 13px; border-right: 0;margin: 0 0 0 1.6em;text-indent: -.6em; padding: 0;");
  195.   this.createCSSRule ("div.aardvarkdbox span.tag", "color: #c00;font-weight:bold;");
  196.   this.createCSSRule ("div.aardvarkdbox span.pname", "color: #080;font-weight: bold;");
  197.   this.createCSSRule ("div.aardvarkdbox span.pval", "color:#00a;font-weight: bold;");
  198.   this.createCSSRule ("div.aardvarkdbox span.aname", "color: #050;font-style: italic;font-weight: normal;");
  199.   this.createCSSRule ("div.aardvarkdbox span.aval", "color:#007;font-style: italic;font-weight: normal;");
  200.   this.doc.didViewSourceDboxCss = true;
  201.   }
  202. dbox.show ();
  203. return true;
  204. },
  205.  
  206. //------------------------------------------------------------
  207.  
  208. colorize : function (elem) {
  209. elem.style.backgroundColor = "#" + 
  210.     Math.floor(Math.random()*16).toString(16) + 
  211.     Math.floor(Math.random()*16).toString(16) + 
  212.     Math.floor(Math.random()*16).toString(16);
  213. elem.style.backgroundImage = "";
  214. return true;
  215. },
  216.  
  217. //------------------------------------------------------------
  218. removeElement : function (elem) {
  219. if (elem.parentNode != null) {
  220.   var tmpUndoData = {
  221.     next : this.undoData,
  222.     mode : 'R',
  223.     elem : elem,
  224.     parent : elem.parentNode,
  225.     nextSibling : elem.nextSibling
  226.     };
  227.   this.undoData = tmpUndoData;
  228.   elem.parentNode.removeChild (elem);
  229.   this.clearBox ();
  230.   return true;
  231.   }
  232. return false;
  233. },
  234.  
  235. //------------------------------------------------------------
  236. paste : function (o) {
  237. if (o.parentNode != null) {
  238.   if (this.undoData.mode == "R") {
  239.     e = this.undoData.elem;
  240.     if (e.nodeName == "TR" && o.nodeName != "TR") {
  241.       var t = this.doc.createElement ("TABLE");
  242.       var tb = this.doc.createElement ("TBODY");
  243.       t.appendChild (tb);
  244.       tb.appendChild (e);
  245.       e = t;
  246.       }
  247.     else if (e.nodeName == "TD" && o.nodeName != "TD") {
  248.       var t2 = this.doc.createElement ("DIV");
  249.       
  250.       var len = e.childNodes.length, i, a = [];
  251.   
  252.       for (i=0; i<len; i++)
  253.         a[i] = e.childNodes.item(i);
  254.         
  255.       for (i=0; i<len; i++) {
  256.         e.removeChild(a[i]);
  257.         t2.appendChild (e);
  258.         }     
  259.       t2.appendChild (e);
  260.       e = t2;    
  261.       }
  262.       
  263.     if (o.nodeName == "TD" && e.nodeName != "TD")
  264.       o.insertBefore (e, o.firstChild);
  265.     else if (o.nodeName == "TR" && e.nodeName != "TR")
  266.       o.insertBefore (e, o.firstChild.firstChild);
  267.     else
  268.       o.parentNode.insertBefore (e, o);
  269.     this.clearBox ();
  270.     this.undoData = this.undoData.next;
  271.     }
  272.   }
  273. return true;
  274. },
  275.  
  276. //------------------------------------------------------------
  277. isolateElement : function (o) {
  278. if (o.parentNode != null) {
  279.   this.clearBox ();
  280.  
  281.   var clone;
  282.   
  283.   if (document.all) {
  284.     // this hack prevents a crash on cnn.com
  285.     if (o.tagName == "TR" || o.tagName == "TD") {
  286.       var t = this.doc.createElement ("TABLE");
  287.       var tb = this.doc.createElement ("TBODY");
  288.       t.appendChild (tb);
  289.     
  290.       if (o.tagName == "TD") {
  291.         var tr = this.doc.createElement ("TR");
  292.         var td = this.doc.createElement ("TD");
  293.         td.innerHTML = o.innerHTML;
  294.         tr.appendChild (td);
  295.         tb.appendChild (tr);
  296.         }
  297.       else {
  298.         var tr = this.doc.createElement ("TR");
  299.         var len = o.childNodes.length;
  300.   
  301.         for (var i=0; i<len; i++) {
  302.           var td = o.childNodes.item(i);
  303.           if (td.nodeName == "TD") {
  304.             var newTd = this.doc.createElement ("TD");
  305.             newTd.innerHTML = td.innerHTML;
  306.             tr.appendChild (newTd);
  307.             }
  308.           }
  309.         tb.appendChild (tr);
  310.         }
  311.       clone = t;
  312.       }
  313.     else {
  314.       var div = document.createElement ("DIV");
  315.       div.innerHTML = o.outerHTML;
  316.       clone = div.firstChild;
  317.       }
  318.     }
  319.   else {
  320.     clone = o.cloneNode (true);
  321.     }
  322.   
  323.   clone.style.textAlign = "";
  324.   clone.style.cssFloat = "none";
  325.   clone.style.styleFloat = "none";
  326.   clone.style.position = "";
  327.   clone.style.padding = "5px";
  328.   clone.style.margin = "5px";
  329.     
  330.   if (clone.tagName == "TR" || clone.tagName == "TD") {
  331.     if (clone.tagName == "TD") {
  332.       var tr = this.doc.createElement ("TR");
  333.       tr.appendChild (clone);
  334.       clone = tr;
  335.       }
  336.     var t = this.doc.createElement ("TABLE");
  337.     var tb = this.doc.createElement ("TBODY");
  338.     t.appendChild (tb);
  339.     tb.appendChild (clone);
  340.     clone = t;
  341.     }
  342.       
  343.   var tmpUndoData = [];
  344.   var len = this.doc.body.childNodes.length, i, count = 0, e;
  345.   
  346.   for (i=0; i<len; i++) {
  347.     e = this.doc.body.childNodes.item(i);
  348.     if (!e.isAardvark) {
  349.       tmpUndoData[count] = e;
  350.       count++;
  351.       }
  352.     }
  353.   tmpUndoData.numElems = count;
  354.     
  355.   for (i=count-1; i>=0; i--)
  356.     this.doc.body.removeChild (tmpUndoData[i]);
  357.   
  358.   tmpUndoData.mode = 'I';
  359.   tmpUndoData.bg = this.doc.body.style.background;
  360.   tmpUndoData.bgc = this.doc.body.style.backgroundColor;
  361.   tmpUndoData.bgi = this.doc.body.style.backgroundImage;
  362.   tmpUndoData.m = this.doc.body.style.margin;
  363.   tmpUndoData.ta = this.doc.body.style.textAlign;
  364.   tmpUndoData.next = this.undoData;
  365.   this.undoData = tmpUndoData;
  366.   
  367.   this.doc.body.style.width = "100%";
  368.   this.doc.body.style.background = "none";
  369.   this.doc.body.style.backgroundColor = "white";
  370.   this.doc.body.style.backgroundImage = "none";
  371.   this.doc.body.style.textAlign = "left";
  372.   
  373.   this.doc.body.appendChild (clone);
  374.  
  375.   //this.makeElems ();
  376.   this.window.scroll (0, 0);
  377.   }
  378. return true;
  379. },
  380.  
  381. //-------------------------------------------------
  382. deWidthify : function (node, skipClear) {
  383. switch (node.nodeType) {
  384.   case 1: // ELEMENT_NODE
  385.     {
  386.     if (node.tagName != "IMG") {
  387.       node.style.width = 'auto';
  388.       if (node.width)
  389.         node.width = null;
  390.       }
  391.     var isLeaf = (node.childNodes.length == 0 && this.leafElems[node.nodeName]);
  392.     
  393.     if (!isLeaf)
  394.       for (var i=0; i<node.childNodes.length; i++)
  395.         this.deWidthify (node.childNodes.item(i));
  396.     }
  397.     break;
  398.   }
  399. if (!skipClear)
  400.   this.clearBox ();
  401. return true;
  402. },
  403.  
  404. //--------------------------------------------------------
  405. blackOnWhite : function (node, isLink) {
  406. // this could be done way better using the createCSSRule thing
  407. switch (node.nodeType) {
  408.   case 1: // ELEMENT_NODE
  409.     {
  410.     if (node.tagName != "IMG") {
  411.       if (node.tagName == "A")
  412.         isLink = true;
  413.       node.style.color = "#000";
  414. //      node.style.color = (isLink)?"#006":"#000";
  415.       if (isLink)
  416.         node.style.textDecoration = "underline";
  417.       node.style.backgroundColor = "#fff";
  418.       node.style.fontFamily = "arial";
  419.       node.style.fontSize = "13px";
  420.       node.style.textAlign = "left";
  421.       node.align = "left";
  422.       node.style.backgroundImage = "";
  423.  
  424.       var isLeaf = (node.childNodes.length == 0 && this.leafElems[node.nodeName]);
  425.     
  426.       if (!isLeaf)
  427.         for (var i=0; i<node.childNodes.length; i++)
  428.           this.blackOnWhite(node.childNodes.item(i), isLink);
  429.       }
  430.     }
  431.     break;
  432.   }
  433. return true;
  434. },
  435.  
  436. //--------------------------------------------------------
  437. getOuterHtmlFormatted : function (node, indent) {
  438. var str = "";
  439.  
  440. if (this.doc.all) {
  441.   return "<pre>" + node.outerHTML.replace(/\</g, '<').replace(/\>/g, '>') + "</pre>"; 
  442.   }
  443.   
  444. switch (node.nodeType) {
  445.   case 1: // ELEMENT_NODE
  446.     {
  447.     if (node.style.display == 'none')
  448.       break;
  449.     var isLeaf = (node.childNodes.length == 0 && this.leafElems[node.nodeName]);
  450.     var isTbody = (node.nodeName == "TBODY" && node.attributes.length == 0);
  451.     
  452.     if (isTbody) {
  453.       for (var i=0; i<node.childNodes.length; i++)
  454.         str += this.getOuterHtmlFormatted(node.childNodes.item(i), indent);
  455.       }
  456.     else {
  457.       if (isLeaf)
  458.         str += "\n<div class='vsindent'>\n";
  459.       else if (indent>0)
  460.         str += "\n<div class='vsblock' style=''>\n<div class='vsline'>\n";
  461.       else
  462.         str += "\n<div class='vsline'>\n";
  463.       
  464.       str += "<<span class='tag'>" +
  465.             node.nodeName.toLowerCase() + "</span>";
  466.       for (var i=0; i<node.attributes.length; i++) {
  467.         if (node.attributes.item(i).nodeValue != null &&
  468.           node.attributes.item(i).nodeValue != '') {
  469.           str += " <span class='pname'>"
  470.           str += node.attributes.item(i).nodeName;
  471.           
  472.           if (node.attributes.item(i).nodeName == "style") {
  473.             var styles = "";
  474.             var a = node.attributes.item(i).nodeValue.split(";");
  475.             for (var j=0; j<a.length; j++) {
  476.               var pair = a[j].split (":");
  477.               if (pair.length == 2) {
  478.                 var s = this.trimSpaces(pair[0]), index;
  479.                 styles += "; <span class='aname'>" + s + "</span>: <span class='aval'>" + this.trimSpaces(pair[1]) + "</span>";
  480.                 }
  481.               }
  482.             styles = styles.substring (2);
  483.             str += "</span>=\"" +  styles + "\"";
  484.             }
  485.           else {
  486.             str += "</span>=\"<span class='pval'>" +  node.attributes.item(i).nodeValue + "</span>\"";
  487.             }
  488.           }
  489.         }
  490.       if (isLeaf)
  491.         str += " />\n</div>\n";
  492.       else {
  493.         str += ">\n</div>\n";
  494.         for (var i=0; i<node.childNodes.length; i++)
  495.           str += this.getOuterHtmlFormatted(node.childNodes.item(i), indent+1);
  496.         str += "\n<div class='vsline'>\n</<span class='tag'>" +
  497.           node.nodeName.toLowerCase() + "</span>>\n</div>\n</div>\n"
  498.         }
  499.       }
  500.     }
  501.     break;
  502.       
  503.   case 3: //TEXT_NODE
  504.     {
  505.     var v = node.nodeValue;
  506.     v = v.replace ("<", "&lt;").replace (">", "&gt;"); 
  507.     
  508.     v = this.trimSpaces (v);
  509.     if (v != '' && v != '\n' 
  510.         && v != '\r\n' && v.charCodeAt(0) != 160)
  511.       str += "<div class='vsindent'>" + v + "</div>";
  512.     }
  513.     break;
  514.     
  515.   case 4: // CDATA_SECTION_NODE
  516.     str += "<div class='vsindent'><![CDATA[" + node.nodeValue + "]]></div>";
  517.     break;
  518.         
  519.   case 5: // ENTITY_REFERENCE_NODE
  520.     str += "&" + node.nodeName + ";<br>"
  521.     break;
  522.  
  523.   case 8: // COMMENT_NODE
  524.     str += "<div class='vsindent'><!--" + node.nodeValue + "--></div>"
  525.     break;
  526.   }
  527. return str;
  528. },
  529.  
  530. camelCaseProps : {
  531.   'colspan': 'colSpan',
  532.   'rowspan': 'rowSpan',
  533.   'accesskey': 'accessKey',
  534.   'class': 'className',
  535.   'for': 'htmlFor',
  536.   'tabindex': 'tabIndex',
  537.   'maxlength': 'maxLength',
  538.   'readonly': 'readOnly',
  539.   'frameborder': 'frameBorder',
  540.   'cellspacing': 'cellSpacing',
  541.   'cellpadding': 'cellPadding'
  542. },
  543.  
  544. //--------------------------------------------------------
  545. domJavascript : function (node, indent) {
  546. var indentStr = "";
  547. for (var c=0; c<indent; c++)
  548.   indentStr += "  ";
  549.   
  550. switch (node.nodeType) {
  551.   case 1: // ELEMENT_NODE
  552.     {
  553.     if (node.style.display == 'none')
  554.       break;
  555.       
  556.     var isLeaf = (node.childNodes.length == 0 && this.leafElems[node.nodeName]);  
  557.     
  558.     var children = "", numChildren = 0, t, useInnerHTML = false;
  559.     if (!isLeaf) {
  560.       for (var i=0; i<node.childNodes.length; i++) {
  561.         t = this.domJavascript(node.childNodes.item(i), indent+1);
  562.         if (t == "useInnerHTML") {
  563.           useInnerHTML = true;
  564.           break;
  565.           }
  566.         if (t) {
  567.           children += indentStr + "  " + t + ",\n";
  568.           numChildren++;
  569.           }
  570.         }
  571.       //  children = indentStr + "   [\n" + children.substring(0, children.length-2) + "\n" + indentStr + "   ]\n"; 
  572.       if (numChildren && !useInnerHTML)
  573.         children = children.substring(0, children.length-2) + "\n"; 
  574.       }
  575.  
  576.     var properties = "", styles = "", numProps = 0, sCount = 0;
  577.     
  578.     for (var i=0; i<node.attributes.length; i++) {
  579.       if (node.attributes.item(i).nodeValue != null && node.attributes.item(i).nodeValue != '') {
  580.         var n = node.attributes.item(i).nodeName,
  581.            v = node.attributes.item(i).nodeValue;
  582.           
  583.         switch (n) {
  584.           case "style": {
  585.             var a = node.attributes.item(i).nodeValue.split(";");
  586.             for (var j=0; j<a.length; j++) {
  587.               var pair = a[j].split (":");
  588.               if (pair.length == 2) {
  589.                 var s = this.trimSpaces(pair[0]), index;
  590.                 while ((index = s.indexOf("-")) != -1)
  591.                  s = s.substring(0, index) + s.charAt(index+1).toUpperCase() + s.substring(index+2);
  592.                  
  593.                 if (s == "float") { // yuk
  594.                  styles += ", <span style='color:#060; font-style:italic'>styleFloat</span>: \"<span style='color:#008;font-style:italic'>" + this.trimSpaces(pair[1]) + "</span>\", <span style='color:#060; font-style:italic'>cssFloat</span>: \"<span style='color:#008;font-style:italic'>" + this.trimSpaces(pair[1]) + "</span>\"";
  595.                  }
  596.                 else {
  597.                  styles += ", <span style='color:#060; font-style:italic'>" + s + "</span>: \"<span style='color:#008;font-style:italic'>" + this.trimSpaces(pair[1]) + "</span>\"";
  598.                  }
  599.                 sCount++;
  600.                 }
  601.               }
  602.             styles = styles.substring (2);
  603.             break;
  604.             }
  605.           default:
  606.             {
  607.             var newN;
  608.             if ((newIn = this.camelCaseProps[n]) != null)
  609.               n = newIn;
  610.             properties += ", <span style='color:#080;font-weight: bold'>" + n + "</span>:\"<span style='color:#00b;font-weight: bold'>" + v + "</span>\"";
  611.             numProps++;
  612.             break;
  613.             }
  614.           }
  615.         }
  616.       }
  617.       
  618.     if (useInnerHTML) {
  619.       var ih = node.innerHTML, index;
  620.       
  621.       if ((index = ih.indexOf("useInnerHTML")) != -1) {
  622.         ih = ih.substring(index + "useInnerHTML".length);
  623.         if (index = ih.indexOf("->") != -1)
  624.           ih = ih.substring(index+3);
  625.         }
  626.       
  627.       properties += ", <span style='color:#080;font-weight: bold'>innerHTML</span>:\"<span style='color:#00b;font-weight: bold'>" +  this.escapeForJavascript (ih) + "</span>\"";
  628.       numProps++;      
  629.       numChildren = 0;
  630.       }
  631.       
  632.     if (styles != "") {
  633.       properties = "{<span style='color:#080;font-weight: bold'>style</span>:{" + styles + "}" + properties + "}";
  634.       numProps++;
  635.       }
  636.     else
  637.       properties = "{" + properties.substring(2) + "}";
  638.     
  639.     // element does not start with an indent, does not end with a linefeed or comma
  640.     // children string starts with indent, has indent for each child
  641.  
  642.     str = "<span style='color:red;font-weight:bold'>" + node.nodeName + "</span> (";
  643.  
  644.     if (numChildren)
  645.       if (numProps)
  646.         return str + properties + ",\n" + children + indentStr + ")";
  647.       else
  648.         return str + "\n" + children + indentStr + ")";
  649.     else
  650.       if (numProps)
  651.         return str + properties  + ")";
  652.       else
  653.         return str + ")";
  654.     }
  655.     break;
  656.       
  657.   case 3: //TEXT_NODE
  658.     {
  659.     var n = node.nodeValue;
  660.     if (node.nodeValue != '')
  661.       n = this.escapeForJavascript (n);   
  662.       
  663.     n = this.trimSpaces (n);
  664.     if (n.length > 0)
  665.       return "\"<b>" + n + "</b>\"";
  666.     }
  667.     break;
  668.     
  669.   case 4: // CDATA_SECTION_NODE
  670.     break;
  671.         
  672.   case 5: // ENTITY_REFERENCE_NODE
  673.     break;
  674.  
  675.   case 8: // COMMENT_NODE
  676.     if (node.nodeValue.indexOf("useInnerHTML") != -1)
  677.       return "useInnerHTML";
  678.     break;
  679.   }
  680. return null;
  681. },
  682.  
  683. //------------------------------------------------------------
  684. makeJavascript : function (elem) {
  685. var dbox = new AardvarkDBox ("#fff", true, false, false, this.strings.javascriptDomCode, true);
  686. dbox.innerContainer.innerHTML = "<pre style=\"margin:3; width: 97%\">" + this.domJavascript(elem, 0) + "</pre><br>";
  687. dbox.show ();
  688. return true;
  689. },
  690.  
  691. //-------------------------------------------------
  692. undo : function () {
  693. if (this.undoData == null)
  694.   return false;
  695.  
  696. this.clearBox ();
  697. var ud = this.undoData;
  698. switch (ud.mode) {
  699.   case "I": {
  700.     var a = [];
  701.     var len = this.doc.body.childNodes.length, i, count = 0, e;
  702.     
  703.     for (i=0; i<len; i++)
  704.       {
  705.       e = this.doc.body.childNodes.item (i);
  706.       if (!e.isAardvark)
  707.         {
  708.         a[count] = e;
  709.         count++;
  710.         }
  711.       }
  712.     for (i=count-1; i>=0; i--)
  713.       this.doc.body.removeChild (a[i]);
  714.       
  715.     len = this.undoData.numElems;
  716.     for (i=0; i<len; i++)
  717.       this.doc.body.appendChild (this.undoData[i]);
  718.  
  719.     this.doc.body.style.background = this.undoData.bg;
  720.     this.doc.body.style.backgroundColor = this.undoData.bgc;
  721.     this.doc.body.style.backgroundImage = this.undoData.bgi;
  722.     this.doc.body.style.margin = this.undoData.m;
  723.     this.doc.body.style.textAlign = this.undoData.ta;
  724.     break;
  725.     }
  726.   case "R": {
  727.     if (ud.nextSibling)
  728.       ud.parent.insertBefore (ud.elem, ud.nextSibling);
  729.     else
  730.       ud.parent.appendChild (ud.elem);
  731.     break;
  732.     }
  733.   default:
  734.     return false;
  735.   }
  736. this.undoData = this.undoData.next; 
  737. return true;
  738. },
  739.  
  740. //-------------------------------------------------
  741. showMenu : function () {
  742. if (this.helpBoxId) {
  743.   if (this.killDbox (this.helpBoxId) == true) {
  744.     delete (this.helpBoxId);
  745.     return;
  746.     }
  747.   }
  748. var s = "<table style='margin:5px 10px 0 10px'>";
  749. for (var i=0; i<this.keyCommands.length; i++) {
  750.   s += "<tr><td style='padding: 3px 7px; border: 1px solid black; font-family: courier; font-weight: bold;" +
  751.     "background-color: #fff'>" + this.keyCommands[i].keystroke +
  752.     "</td><td style='padding: 3px 7px; font-size: .9em;  text-align: left;'>" + this.keyCommands[i].name + "</td></tr>";
  753.   }
  754. s += "</table><br>" + this.strings.karmaticsPlug; 
  755.   
  756. var dbox = new AardvarkDBox ("#fff2db", true, true, true, this.strings.aardvarkKeystrokes);
  757. dbox.innerContainer.innerHTML = s;
  758. dbox.show ();
  759. helpBoxId = dbox.id;
  760. return true;
  761. },
  762.  
  763.  
  764. //------------------------------------------------------------
  765. getByKey : function (key) {
  766. var s = key + " - ";
  767. for (var i=0; i<this.keyCommands.length; i++) {
  768.     s += this.keyCommands[i].keystroke;
  769.     if (this.keyCommands[i].keystroke == key) {
  770.         return this.keyCommands[i];
  771.         }
  772.     }
  773. return null;
  774. },
  775.  
  776. //------------------------------------------------------------
  777. getElementXPath: function(elem) {
  778.   var path = "";
  779.   for (; elem && elem.nodeType == 1; elem = elem.parentNode) {
  780.     var index = 1;
  781.     for (var sib = elem.previousSibling; sib; sib = sib.previousSibling) {
  782.       if (sib.nodeType == 1 && sib.tagName == elem.tagName)
  783.         index++;
  784.       }
  785.     var xname = "xhtml:" + elem.tagName.toLowerCase();
  786.     if (elem.id) {
  787.       xname += "[@id='" + elem.id + "']";
  788.     } else {
  789.       if (index > 1)
  790.         xname += "[" + index + "]";
  791.     }
  792.     path = "/" + xname + path;
  793.   }
  794. var dbox = new AardvarkDBox ("#fff", true, false, false, "xPath", true);
  795. dbox.innerContainer.innerHTML = "<pre wrap=\"virtual\" style=\"margin:3; width: 97%\">" + path + "</pre><br>";
  796. dbox.show ();
  797. },
  798.  
  799. //--------------------------------------------------------
  800. // make a global variable, available to javascript running inside the page
  801. // handy tool for javascript developers
  802. // The bookmarklet version also adds a function to the element that iterates 
  803. // the descendents
  804. makeGlobalFromElement: function(elem) {
  805. if (this.isBookmarklet) {
  806.   for (var i=1; i<100; i++) {
  807.     if (this.window["elem"+i]==undefined) {
  808.       this.window["elem"+i] = elem;
  809.       elem.tree = this.tree;
  810.       var dbox = new AardvarkDBox ("#feb", false, true, true);
  811.       dbox.innerContainer.innerHTML = "<p style='color: #000; margin: 3px 0 0 0;'>global variable \"<b>elem" + i + "</b>\" created</p>";
  812.       dbox.show ();
  813.       setTimeout ("aardvark.killDbox(" + dbox.id + ")", 2000);
  814.       return true;
  815.       }
  816.     }
  817.   }
  818. else {
  819.   // this is kind of a hack to make the variable available to javascript
  820.   // within the page
  821.   if (this.doc.aardvarkElemNum == null)
  822.     this.doc.aardvarkElemNum = 1;
  823.   else
  824.     this.doc.aardvarkElemNum++;
  825.   var removeId = false;
  826.   if (elem.id == null || elem.id == "") {
  827.     elem.id = "aardvarkTmpId" + this.doc.aardvarkElemNum;
  828.     removeId = true;
  829.     }
  830.   var s = "window.elem" + this.doc.aardvarkElemNum + "= document.getElementById('" + elem.id + "');\n";
  831.   if (removeId)
  832.     s += "document.getElementById('" + elem.id + "').id = '';";
  833.   
  834.   var dbox = new AardvarkDBox ("#feb", false, true, true);
  835.   dbox.innerContainer.innerHTML = "<p style='color: #000; margin: 3px 0 0 0;'>global variable \"<b>elem" + this.doc.aardvarkElemNum + "</b>\" created</p>";
  836.   dbox.show ();
  837.   setTimeout ("aardvark.killDbox(" + dbox.id + ")", 2000);
  838.  
  839.   var scriptElem=this.doc.createElement('script');
  840.   scriptElem.type='text/javascript';
  841.   scriptElem.appendChild (this.doc.createTextNode(s));
  842.   var h = this.doc.getElementsByTagName("head")[0];
  843.   h.appendChild(scriptElem);
  844.   return true;
  845.   }
  846. return false;
  847. },
  848.  
  849. //--------------------------------------------------------
  850. getNextElement : function () {
  851. this.index++;
  852. if (this.index < this.list.length) {
  853.   this.depth = this.list[this.index].depth;
  854.   return this.list[this.index].elem;
  855.   }
  856. return null;
  857. },
  858.  
  859. //--------------------------------------------------------
  860. tree : function () {
  861. var t = {
  862.   list: [{elem: this, depth: 0}],
  863.   index: -1,
  864.   depth: 0,
  865.   next: aardvark.getNextElement
  866.   };
  867. aardvark.addChildren (this, t, 1);
  868. return t;
  869. },
  870.  
  871. //--------------------------------------------------------
  872. addChildren : function (elem, t, depth) {
  873.   for (var i=0; i<elem.childNodes.length; i++) {
  874.     var child = elem.childNodes[i];
  875.     if (child.nodeType == 1) {
  876.       t.list.push({elem: child, depth: depth});
  877.       if (child.childNodes.length != 0 && !aardvark.leafElems[child.nodeName])
  878.         aardvark.addChildren(child, t, depth + 1);
  879.       }
  880.     }
  881.   }
  882.  
  883. });
  884.  
  885.